package com.app.micaihu.view.army.presenter

import com.app.base.mvp.BasePresenter
import com.app.http.NetRequestListener
import com.app.http.bean.BaseBean
import com.app.micaihu.bean.news.ShareBean
import com.app.micaihu.utils.ApiServiceUtils.apiService
import com.app.micaihu.utils.ParamsMapUtils
import com.app.micaihu.view.army.contract.ArmyDetailContract
import com.app.micaihu.view.bean.ArmyDetail
import com.app.micaihu.view.bean.ArmyUpgrade
import com.app.utils.biz.Biz

/**
 * 军团详情
 */
open class ArmyDetailPresenter : BasePresenter<ArmyDetailContract.View>(), ArmyDetailContract.Presenter {

    override fun detail(toUid: String, gid: String) {
        getView()?.showLoading()
        val params = ParamsMapUtils.commonParams
        params["gid"] = gid
        params["uid"] = toUid
        requestNetwork(apiService.armyDetail(params), object : NetRequestListener<BaseBean<ArmyDetail?>?> {
            override fun onSuccess(result: BaseBean<ArmyDetail?>?) {
                getView()?.hideLoading()
                getView()?.handleDetail(result)
            }

            override fun onFailed(e: Throwable?) {
                getView()?.hideLoading()
                getView()?.handleDetail(null)
                getView()?.onError(e)
            }
        })
    }

    override fun share() {
        val params = ParamsMapUtils.commonParams
        params["uid"] = Biz.instance.uid()
        requestNetwork(apiService.armyShare(params), object : NetRequestListener<BaseBean<ShareBean?>?> {
            override fun onSuccess(result: BaseBean<ShareBean?>?) {
                getView()?.hideLoading()
                getView()?.handleShare(result)
            }

            override fun onFailed(e: Throwable?) {
                getView()?.hideLoading()
                getView()?.handleShare(null)
                getView()?.onError(e)
            }
        })
    }


    override fun join(gid: String, msg: String) {
        val params = ParamsMapUtils.commonParams
        params["gid"] = gid
        params["msg"] = msg
        params["uid"] = Biz.instance.uid()
        requestNetwork(apiService.join(params), object : NetRequestListener<BaseBean<Any?>?> {
            override fun onSuccess(result: BaseBean<Any?>?) {
                getView()?.hideLoading()
                getView()?.handleJoin(result)
            }

            override fun onFailed(e: Throwable?) {
                getView()?.hideLoading()
                getView()?.handleJoin(null)
                getView()?.onError(e)
            }
        })
    }

    override fun deleteMember(gid: String, toUid: String) {
        val params = ParamsMapUtils.commonParams
        params["gid"] = gid
        params["to_uid"] = toUid
        params["uid"] = Biz.instance.uid()
        requestNetwork(apiService.deleteMember(params), object : NetRequestListener<BaseBean<Any?>?> {
            override fun onSuccess(result: BaseBean<Any?>?) {
                getView()?.hideLoading()
                getView()?.handleDeleteMember(result)
            }

            override fun onFailed(e: Throwable?) {
                getView()?.hideLoading()
                getView()?.handleDeleteMember(null)
                getView()?.onError(e)
            }
        })
    }

    override fun armyUpgrade(gid: String) {
        getView()?.showLoading()
        val params = ParamsMapUtils.commonParams
        params["gid"] = gid
        params["uid"] = Biz.instance.uid()
        requestNetwork(apiService.armyUpgrade(params), object : NetRequestListener<BaseBean<ArmyUpgrade?>?> {
            override fun onSuccess(result: BaseBean<ArmyUpgrade?>?) {
                getView()?.hideLoading()
                getView()?.handleArmyUpgradeSuccess(result)
            }

            override fun onFailed(e: Throwable?) {
                getView()?.hideLoading()
                getView()?.handleArmyUpgradeSuccess(null)
                getView()?.onError(e)
            }
        })
    }


}